package com.zetyun.tiger.sql.routine;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.zetyun.tiger.sql.pkm.GenSrPkmApp;

import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GenRoutineApp {

    public static void main(String[] args) throws SQLException {
        ArrayList<String> list = new ArrayList<>();

        String host1 = "hdp1";
        String host2 = "hdp2";
        String host3 = "hdp3";
//        String host1 = "rdx238";
//        String host2 = "rdx239";
//        String host3 = "rdx240";

        //查询
        List<Entity> result = Db.use()
                .query("select concat('DKM_ODS_',system_name,'_',tb_name) full_name," +
                        "concat('ODS_',system_name,'_',tb_name) topic_name," +
                        "tb_name from ods_table ot  where tb_name not in('CMMSCCNL','CMTXPTXP','PY_TRANFLOW','TRN_RLTM_GTHR_SRL','EFS_SYS_LOGINFO1','EFS_SYS_LOGINFO2')" );

        String routineSqlTemplate = FileUtil.readString(Objects.requireNonNull(
                GenRoutineApp.class.getResource("/routine.template")), StandardCharsets.UTF_8);

        for (Entity entity : result) {
            String tb_name = entity.getStr("tb_name");
            String full_name = entity.getStr("full_name");
            String topic_name = entity.getStr("topic_name");

            String sql = StrFormatter.format(
                    routineSqlTemplate,
                    full_name,
                    full_name,
                   host1,
                   host2,
                   host3,
                    topic_name,
                    full_name,
                    full_name,
                    full_name,
                    full_name,
                    full_name
            );
            list.add(sql);
        }
        FileUtil.writeLines(list,"routine-load.sql",StandardCharsets.UTF_8);
    }
}
